const { list, add, find } = require('./data/movie-level')

const base = '/movieserver'

module.exports = [{
  url: `${base}/movieLevels`,
  type: 'get',
  response: config => {
    const { name } = config.query
    return list(name)
  }
}, {
  url: `${base}/movieLevels`,
  type: 'post',
  response: config => {
    const { name, price, openPrice, returnMoneySupport, enableHolidayPrice } = config.body
    add({ name, price, openPrice, returnMoneySupport, enableHolidayPrice })
    return { success: true }
  }
}, {
  url: `${base}/movieLevels/[1-9][0-9]*`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const level = find(id)
    if (level) {
      const { name, price, openPrice, returnMoneySupport, enableHolidayPrice } = config.body
      level.name = name
      level.price = price
      level.openPrice = openPrice
      level.returnMoneySupport = returnMoneySupport
      level.enableHolidayPrice = enableHolidayPrice
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}]
